import { CustomColumnGroup } from './model/CustomColumnGroup.ts'
import { sequelize } from './model/sequelize.ts'
import { getFieldsJSONFile, resolve } from './utils.ts'

async function updateColumns(columns) {
  const tasks = columns.map((item) => {
    return CustomColumnGroup.update(
      {
        group_data: item.groupData
      },
      {
        where: {
          group_type: item.column,
          data_type: item.dataType
        }
      }
    ).then(([effectRow]) => {
      console.log(`${item.columnType}: ${item.column}, dataType: ${item.dataType} ${effectRow > 0 ? '已更新' : '未更新'}`);
      return effectRow
    }).catch(err => {
      console.log(err);
    })
  })

  const res = await Promise.all(tasks)

  const taskNum = res.length

  const finishTaskNum = res.filter(i => i > 0).length

  console.log(`\n更新完毕, 共更新了${taskNum}个自定义位置数据, ${finishTaskNum}个已更新，${taskNum - finishTaskNum}个未更新`);

  // 关闭连接
  sequelize.close()
}

const columnsData = getFieldsJSONFile(resolve('../dist/fields'))

updateColumns(columnsData)